App.controller('modelsController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

    $scope.model = {};
    $scope.grid = {
        column_defs: [
			{
            	target: 1,
            	template: function(row){
            		return '<a href="bpm/models/'+row.id+'/export.do" _index="'+row._index+'">'+row.name+'</a>'
            	}
            },
            {
                target: 5,
                buttons: [
                    {
                        name: '设计',
                        class: 'btn-success',
                        icon: 'icon-pencil',
                        event: function(row) {
                            $state.go('auth.bpmDesign', {modelId: row.id});
                        }
                    },
                    {
                        name: '部署',
                        class: 'btn purple',
                        icon: 'icon-share',
                        event: function(row, btn) {
                            Page.ajaxPost('bpm/models/' + row.id + '/deploy.do', null, function(resp) {
                                Page.notice('发布成功！');
                                btn.addClass('disabled');
                            });
                        }
                    }                    
                ]
            }
        ],
        update: function(row) {
            $scope.model = row;
            $scope.model.metaInfo = angular.fromJson(row.metaInfo).description;
            $scope.grid.edit = true;
        },
        delete: function(row) {
            Page.confirm("确定要删除吗？", function () {
                Page.ajaxDelete('bpm/models/' + row.id + '.do', null, function(resp) {
                    $scope.grid.query();
                });
            });
        },
        save: function () {
            if ($scope.model._add) {// 新增
                Page.ajaxPost('bpm/models.do', $scope.model, function(resp) {
                    $scope.grid.edit = false;
                    $state.go('auth.bpmDesign', {modelId: resp.data});
                });
            } else {// 修改
                Page.ajaxPut('bpm/models/' + $scope.model.id + '.do', $scope.model, function(resp) {
                    $scope.grid.edit = false;
                    $scope.grid.query();
                });
            }
        },
        openImport : function(){
        	$scope.model = {};
        	$scope.grid.deploy = true;
        	$scope.grid.edit = false;
        },
        importFile : function(){
        	var filePath = $scope.model.modelFile && $scope.model.modelFile.path ? $scope.model.modelFile.path : '';
        	if(filePath != ''){
        		Page.ajaxPost('bpm/models/import.do', {category: $scope.model.category,modelFile: filePath}, function(resp) {
        			Page.alert("导入成功！", function() {
        				$scope.grid.deploy = false;
            	    	$scope.grid.edit = false;
            			$scope.grid.query();
                    });
                });
        	}else{
        		Page.notice('文件为空！');
        	}
        },
        cancelImport : function(){
        	$scope.grid.edit = false;
        	$scope.grid.deploy = false;
        }
    };
}]);
